﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace LI4.Data
{
    class Area
    {
        private Ligacao ligacao;

        public Area()
        {
            ligacao = new Ligacao();
        }

        public void Inserir(Business.Area area)
        {
            if (!existeArea(area.Nome))
               ligacao.Update("INSERT [dbo].[Area] ( [nome] ) VALUES (N'" + area.Nome + "')");
        }

        public void Alterar(Area c)
        {

        }
        public void Remover(Area c)
        {

        }

        public ArrayList getAreas()
        {
            ArrayList array = new ArrayList();
            SqlDataReader reader;

            ligacao.Select("SELECT cod_area,nome FROM Area ORDER BY nome");
            reader = ligacao.mySqlDataReader;

            while (reader.Read())
                array.Add(new Business.Area(reader.GetInt32(0), reader.GetString(1)));

            reader.Close();
            return array;
        }

        public bool existeArea(string nome)
        {
            SqlDataReader reader;

            ligacao.Select("SELECT nome FROM Area where nome='" + nome + "';");
            reader = ligacao.mySqlDataReader;
            if (reader.Read())
            {
                reader.Close();
                return true;
            }
            else
            {
                reader.Close();
                return false;
            }
        }

        public Business.Area getArea(int codigo)
        {
            SqlDataReader reader;
            Data.Distrito distData = new Data.Distrito();

            ligacao.Select("SELECT nome FROM Area where cod_area='" + codigo + "'");
            reader = ligacao.mySqlDataReader;
            reader.Read();
            string nome = reader.GetString(0);
            reader.Close();
            

            Business.Area area = new Business.Area(codigo,nome);

            return area;
        }
    }
}
